﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.DataLayer;
using BaHuan.Entities;



namespace BaHuan
{
    public partial class FrmDinhKhoanPhieuChuyenKho : Form
    {
        private CTPhieuChuyenKho ctphieuchuyenkho = new CTPhieuChuyenKho();
        private ButToan buttoan = new ButToan();
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private CTPhieuChuyenKhoManagement ctphieuchuyenkhoMgt = new CTPhieuChuyenKhoManagement();
        private ChiNhanhCongTy chinhanhcongty = new ChiNhanhCongTy();
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();

        public long tien = 0;
        public long tongtien = 0;

        private PhieuChuyenKho phieuchuyenkho;
        public PhieuChuyenKho PhieuChuyenKho
        {
            set { this.phieuchuyenkho = value; }
            get { return this.phieuchuyenkho; }
        }

        public FrmDinhKhoanPhieuChuyenKho(long tient)
        {
            InitializeComponent();
            this.tongtien = tient;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < dgvDinhKhoan.Rows.Count - 1; i++)
            {
                if ((dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString() == "" || dgvDinhKhoan.Rows[0].Cells[2].FormattedValue.ToString() == "" || dgvDinhKhoan.Rows[i].Cells[1].FormattedValue.ToString() == null || dgvDinhKhoan.Rows[i].Cells[2].FormattedValue.ToString() == null))
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin trong danh sách !", "Thông báo");
                    return;
                }
            }

            if (tien == tongtien)
            {
                EditOrRemoveDinhKhoan(phieuchuyenkho.MaSo);
                ResultObj resultObj = new ResultObj();

                ButToan buttoan = new ButToan();
                buttoan.NgayThang = dtpNgay.Value;
                buttoan.DienGiai = txtNoiDung.Text;
                buttoan.MaSoKhachHang = cboChiNhanh.Text;
                buttoan.ChiNhanh = cboChiNhanh.Text;
                buttoan.TKGoc = dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString();
                buttoan.TKDoiUng = dgvDinhKhoan.Rows[0].Cells[2].FormattedValue.ToString();
                buttoan.SoChungTu = txtSoChungTu.Text;
                buttoan.GhiNo = 0;
                buttoan.GhiCo = Convert.ToInt64(dgvDinhKhoan.Rows[0].Cells[4].Value.ToString());

                resultObj = buttoanMgt.SaveNewButToan(buttoan);

                buttoan = new ButToan();
                buttoan.NgayThang = dtpNgay.Value;
                buttoan.DienGiai = txtNoiDung.Text;
                buttoan.MaSoKhachHang = cboChiNhanh.Text;
                buttoan.ChiNhanh = cboChiNhanh.Text;
                buttoan.TKGoc = dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString();
                buttoan.TKDoiUng = dgvDinhKhoan.Rows[0].Cells[1].FormattedValue.ToString();
                buttoan.SoChungTu = txtSoChungTu.Text;
                buttoan.GhiNo = Convert.ToInt64(dgvDinhKhoan.Rows[0].Cells[4].Value.ToString());
                buttoan.GhiCo = 0;

                resultObj = buttoanMgt.SaveNewButToan(buttoan);

                if (resultObj.ReturnCode > 0)
                {
                    MessageBox.Show("Lưu Thành Công", "Thông Báo");
                    this.Close();
                }
                else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                else
                    MessageBox.Show("Thất bại");
            }
            else
            {
                MessageBox.Show("Số tiền định khoản không đúng", "Thông báo");
            }
        }

        public void EditOrRemoveDinhKhoan(string MaSo)
        {
            ResultObj resultObj = new ResultObj();

            List<ButToan> lst = (from p in GetAllButToan()
                                 where p.SoChungTu == MaSo
                                 select p).ToList();

            if (lst.Count > 0)
            {
                foreach (ButToan b in lst)
                {
                    b.IsEdited = 1;
                    resultObj = buttoanMgt.UpdateButToan(b);
                }
            }
        }

        public void BindingToCbp()
        {
            cboChiNhanh.DataSource = GetAllChiNhanhCongTy();
            cboChiNhanh.DisplayMember = "TenChiNhanh";
            cboChiNhanh.ValueMember = "Id";

            cboTKCo.DataSource = GetAllTaiKhoanKeToan();
            cboTKCo.DisplayMember = "MaSoTK";
            cboTKCo.ValueMember = "TenTK";

            cboTKNo.DataSource = GetAllTaiKhoanKeToan();
            cboTKNo.DisplayMember = "MaSoTK";
            cboTKNo.ValueMember = "TenTK";
        }

        private void FrmDinhKhoanPhieuChuyenKho_Load(object sender, EventArgs e)
        {
            dgvDinhKhoan.AllowUserToAddRows = false;
            gbChiTietDK.Enabled = true;

            BindingToCbp();

            ResultObj resultObj = new ResultObj();
            resultObj = ctphieuchuyenkhoMgt.GetAllCTPhieuChuyenKho();

            List<CTPhieuChuyenKho> lstctPhieuChuyenKho = (List<CTPhieuChuyenKho>)resultObj.Result;
            txtMSKH.Text = phieuchuyenkho.KhoNhan;
            txtSoChungTu.Text = phieuchuyenkho.MaSo;
            txtNoiDung.Text = phieuchuyenkho.LyDo;
            cboChiNhanh.SelectedIndex = cboChiNhanh.FindString(this.phieuchuyenkho.KhoXuat);

            txtMSKH.ReadOnly = true;
            txtSoChungTu.ReadOnly = true;
            txtNoiDung.ReadOnly = true;
            cboChiNhanh.Enabled = false;
            dgvDinhKhoan.Columns["colSoTien"].ReadOnly = false;

            DataGridViewRow row = new DataGridViewRow();
            DataGridViewCell cell; 
            DataGridViewComboBoxCell cboCell;

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = GetAllTaiKhoanKeToan();
            TaiKhoanKeToan tkkt = new TaiKhoanKeToan();
            for (int i = 0; i < lstTaiKhoanKeToan.Count; i++)
                if (lstTaiKhoanKeToan[i].MaSoTK == "155")
                    tkkt = lstTaiKhoanKeToan[i];

            long tong = 0;
            for (int i = 0; i < this.phieuchuyenkho.listCTPhieuChuyenKho.Count; i++)
            {
                row = new DataGridViewRow();
                cell = new DataGridViewTextBoxCell();
                cell.Value = this.phieuchuyenkho.listCTPhieuChuyenKho[i].TenHang;
                row.Cells.Add(cell);

                cboCell = new DataGridViewComboBoxCell();
                cboCell.DataSource = lstTaiKhoanKeToan;
                cboCell.DisplayMember = "MaSoTk";
                cboCell.ValueMember = "TenTK";
                cboCell.Value = tkkt.TenTK;
                row.Cells.Add(cboCell);

                cboCell = new DataGridViewComboBoxCell();
                cboCell.DataSource = lstTaiKhoanKeToan;
                cboCell.DisplayMember = "MaSoTk";
                cboCell.ValueMember = "TenTK";
                cboCell.Value = tkkt.TenTK;
                row.Cells.Add(cboCell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = tkkt.TenTK;
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = this.phieuchuyenkho.listCTPhieuChuyenKho[i].ThanhTien;
                tong += this.phieuchuyenkho.listCTPhieuChuyenKho[i].ThanhTien;
                tien += this.phieuchuyenkho.listCTPhieuChuyenKho[i].ThanhTien;
                row.Cells.Add(cell);

                dgvDinhKhoan.Rows.Add(row);
            }

            dgvDinhKhoan.Rows[0].Cells[4].Value = tong;
        }

        private void dgvDinhKhoan_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.RowIndex > -1)
            {
                dgvDinhKhoan.Rows[e.RowIndex].Cells[1].ReadOnly = true;
                dgvDinhKhoan.Rows[e.RowIndex].Cells[2].ReadOnly = true;
                ResultObj reultObj = new ResultObj();
                string tenTK = dgvDinhKhoan.Rows[e.RowIndex].Cells[1].Value.ToString();
                dgvDinhKhoan.Rows[e.RowIndex].Cells[3].Value = tenTK;
                int soDinhKhoan = Convert.ToInt32(dgvDinhKhoan.Rows[e.RowIndex].Cells[1].FormattedValue.ToString());
            }

            if (e.ColumnIndex == 4 && e.RowIndex > -1)
            {
                if (dgvDinhKhoan.Rows[e.RowIndex].Cells[4].Value == null)
                    dgvDinhKhoan.Rows[e.RowIndex].Cells[4].Value = 0;
            }

            long tongMotNamNam = 0;
            for (int i = 1; i < dgvDinhKhoan.Rows.Count; i++)
            {

                if (dgvDinhKhoan.Rows[i].Cells[4].Value != null)
                {
                    tongMotNamNam += Convert.ToInt64(dgvDinhKhoan.Rows[i].Cells[4].Value.ToString());
                }
            }

            tien = tongMotNamNam;
            dgvDinhKhoan.Rows[0].Cells[4].Value = tongMotNamNam;
        }

        private void dgvDinhKhoan_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = false;
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvDinhKhoan.CurrentRow != null)
            {
                if (dgvDinhKhoan.CurrentRow.Index != (dgvDinhKhoan.Rows.Count - 1))
                {
                    dgvDinhKhoan.Rows.RemoveAt(dgvDinhKhoan.CurrentRow.Index);
                    long sotien = Convert.ToInt64(dgvDinhKhoan.Rows[dgvDinhKhoan.CurrentRow.Index].Cells[3].Value.ToString());
                    long sotienTKGoc = Convert.ToInt64(dgvDinhKhoan.Rows[0].Cells[3].Value.ToString());
                    long result = sotienTKGoc - sotien;
                    this.tongtien = result;
                    dgvDinhKhoan.Rows[0].Cells[3].Value = result;
                }
            }
        }

        private void dgvDinhKhoan_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvDinhKhoan.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvDinhKhoan.ClearSelection();
                    dgvDinhKhoan.CurrentCell = dgvDinhKhoan.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
            }
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return listChiNhanhCongTy;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resulObj = new ResultObj();
            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();
            resulObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resulObj.Result;
            return lstTaiKhoanKeToan;

        }
    }
}
